home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / llist10.zip / TEST.PAS < prev   
Pascal/Delphi Source File  |  1990-01-14  |  5KB  |  190 lines

  1. PROGRAM test2;
  2.  
  3. {Test program for unit llist (doubly-linked list).  This is not an 
  4. exhaustive test, but it does exercise most of the llist unit fairly 
  5. well.}
  6.  
  7.  
  8. USES
  9.   llist;
  10.  
  11.  
  12. {Here are the nodes I want to keep on the list.  I want to store integers
  13. on the list, so I've defined "data" as an integer.  I've also created my
  14. own constructor, which sets the "data" field when the node is created.}
  15.  
  16. TYPE
  17.   my_linked_list_ptr = ^my_linked_list_node;
  18.   my_linked_list_node =
  19.     OBJECT (linked_list_node)
  20.     data: Integer;
  21.     CONSTRUCTOR init (value: Integer);
  22.     END;
  23.  
  24.  
  25. {----- Methods for my_linked_list_node -----}
  26.  
  27. {Here's the constructor for the nodes I'm storing on the list.  The
  28. default constructor is called, and then the "data" field is set.}
  29.  
  30. CONSTRUCTOR my_linked_list_node.init (value: Integer);
  31. BEGIN
  32.   linked_list_node.init;
  33.   data := value
  34. END;
  35.  
  36.  
  37. {----- Rest of stuff -----}
  38.  
  39. {This is the anchor for my linked list.  Every linked list must have an
  40. anchor.  A program may have multiple anchors, for multiple linked lists.
  41. Each node, of course, can only be on one linked list (associated with one
  42. anchor).}
  43.  
  44. VAR
  45.   anchor: linked_list_anchor;
  46.  
  47.  
  48. {Display the value of each node on the list, going from the head of the
  49. list to the tail.}
  50.  
  51. PROCEDURE display_forward;
  52. VAR
  53.   p: my_linked_list_ptr;
  54. BEGIN
  55.   Writeln ('----- Displaying forward; MaxAvail = ', MaxAvail);
  56.   IF anchor.empty THEN
  57.     Writeln ('(empty)')
  58.   ELSE
  59.     BEGIN
  60.     p := my_linked_list_ptr (anchor.get_first);
  61.     WHILE (p <> NIL) DO
  62.       BEGIN
  63.       Writeln (p^.data);
  64.       p := my_linked_list_ptr (p^.get_next)
  65.       END
  66.     END
  67. END;
  68.  
  69.  
  70. {Display the value of each node on the list, going from the tail of the
  71. list to the head.}
  72.  
  73. PROCEDURE display_backward;
  74. VAR
  75.   p: my_linked_list_ptr;
  76. BEGIN
  77.   Writeln ('----- Displaying backward; MaxAvail = ', MaxAvail);
  78.   IF anchor.empty THEN
  79.     Writeln ('(empty)')
  80.   ELSE
  81.     BEGIN
  82.     p := my_linked_list_ptr (anchor.get_last);
  83.     WHILE (p <> NIL) DO
  84.       BEGIN
  85.       Writeln (p^.data);
  86.       p := my_linked_list_ptr (p^.get_prev)
  87.       END
  88.     END
  89. END;
  90.  
  91.  
  92. {Create a node with this value and add it to the head of the list, then
  93. display the list.}
  94.  
  95. PROCEDURE add_head (value: Integer);
  96. VAR
  97.   p: my_linked_list_ptr;
  98. BEGIN
  99.   Writeln ('Adding ', value, ' to head of list');
  100.   New (p, init (value) );
  101.   anchor.add_to_head (p^)
  102. END;
  103.  
  104.  
  105. {Create a node with this value and add it to the tail of the list, then
  106. display the list.}
  107.  
  108. PROCEDURE add_tail (value: Integer);
  109. VAR
  110.   p: my_linked_list_ptr;
  111. BEGIN
  112.   Writeln ('Adding ', value, ' to tail of list');
  113.   New (p, init (value) );
  114.   anchor.add_to_tail (p^)
  115. END;
  116.  
  117.  
  118. {Create a node with this value and add it AFTER the first node in the
  119. list.}
  120.  
  121. PROCEDURE add_after_head (value: Integer);
  122. VAR
  123.   p: my_linked_list_ptr;
  124. BEGIN
  125.   Writeln ('Adding ', value, ' after head');
  126.   New (p, init (value) );
  127.   p^.add_after (anchor.get_first^)
  128. END;
  129.  
  130.  
  131. {Create a node with this value and add it BEFORE the last node in the
  132. list.}
  133.  
  134. PROCEDURE add_before_tail (value: Integer);
  135. VAR
  136.   p: my_linked_list_ptr;
  137. BEGIN
  138.   Writeln ('Adding ', value, ' before tail');
  139.   New (p, init (value) );
  140.   p^.add_before (anchor.get_last^)
  141. END;
  142.  
  143.  
  144. {Remove the node at the head of the list.}
  145.  
  146. PROCEDURE remove_from_head;
  147. BEGIN
  148.   Writeln ('Removing node at head');
  149.   Dispose (anchor.get_first, done)
  150. END;
  151.  
  152.  
  153. {Remove the node at the tail of the list.}
  154.  
  155. PROCEDURE remove_from_tail;
  156. BEGIN
  157.   Writeln ('Removing node at tail');
  158.   Dispose (anchor.get_last, done)
  159. END;
  160.  
  161.  
  162. BEGIN
  163.   Writeln ('MaxAvail = ', MaxAvail);
  164.   anchor.init;                display_forward;
  165.   add_head (-1);              display_forward;
  166.   add_head (-2);              display_forward;
  167.   add_head (-3);              display_forward;
  168.   add_after_head (-9);        display_forward;
  169.   display_backward;
  170.   WHILE NOT anchor.empty DO
  171.     BEGIN
  172.     remove_from_head;         display_forward;
  173.     END;
  174.   add_tail (1);               display_forward ;
  175.   add_tail (2);               display_forward;
  176.   add_tail (3);               display_forward;
  177.   add_before_tail (9);        display_forward;
  178.   WHILE NOT anchor.empty DO
  179.     BEGIN
  180.     remove_from_tail;         display_forward;
  181.     END;
  182.   add_head (10);              display_forward;
  183.   add_head (20);              display_forward;
  184.   Writeln ('Disposing all nodes');
  185.   anchor.dispose_all_nodes;   display_forward;
  186.   Writeln ('Destructing anchor');
  187.   anchor.done;
  188.   Writeln ('MaxAvail = ', MaxAvail)
  189. END.
  190.